package com.foodost.idiomer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalDb.DbUpdateListener;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class IdiomModel {
	private static final String TAG = "IdiomModel";
	private static FinalDb db;
	private String DATABASES_DIR="/data/data/com.foodost.idiomer/databases";
	public static FinalDb getFinalDb(Context context){
		if(db == null){
			db = FinalDb.create(context, Constants.DB_NAME, false, Constants.DB_VERSION, dbListener);
		}
	    return db;
	}
	
    private static DbUpdateListener dbListener = new DbUpdateListener() {
        
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if(newVersion > oldVersion){
                Log.d(TAG, "newVersion = " + newVersion);
            }
        }
    };
    
    public IdiomModel(Context context){
    }
    
    public  void copyDatabaseFile(Context context, boolean isfored) {  
        
        File dir = new File(DATABASES_DIR);  
        if (!dir.exists() || isfored) {  
            try {  
                dir.mkdir();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
          
        File dest = new File(dir, Constants.DB_NAME);  
        if(dest.exists() && !isfored){  
            return ;  
        }  
          
        try {  
            if(dest.exists()){  
                dest.delete();  
            }  
            dest.createNewFile();     
            InputStream in = context.getResources().openRawResource(R.raw.idiom);  
            int size = in.available();  
            byte buf[] = new byte[size];  
            in.read(buf);  
            in.close();  
            FileOutputStream out = new FileOutputStream(dest);  
            out.write(buf);  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
	
	//通过等级和需要个数，返回对应成语集合
	public  List<IdiomData> getIdiomDatas(Context context,int grage,int num){
		List<IdiomData> idiomDatas = new ArrayList<IdiomData>();
		idiomDatas = getFinalDb(context).findAllByWhere(IdiomData.class, "idiomGrage = " + grage);
		Collections.shuffle(idiomDatas);
		Log.d(TAG, "idioms = " + idiomDatas.size());
		if(num == 0){//default
			return idiomDatas;
		}else if(idiomDatas.size() > num){
			return idiomDatas.subList(0, num);
		}
		return idiomDatas;
	}
	
	//打乱成语集
	public  List<String> mixIdiom(IdiomData idiomData){
		List<String> list = new ArrayList<String>();
		Log.d(TAG, "idiom = " + idiomData.toString());
		if(idiomData.getIdiomRight() != null){
			list.add(idiomData.getIdiomRight());
		}
		if(idiomData.getIdiomWrong1() != null){
			list.add(idiomData.getIdiomWrong1());
		}
		if(idiomData.getIdiomWrong2() != null){
			list.add(idiomData.getIdiomWrong2());
		}
		if(idiomData.getIdiomWrong3() != null){
			list.add(idiomData.getIdiomWrong3());
		}
		Collections.shuffle(list);
		return list;
	}
	
	public  String getExplan(Context context,String idiomRight){
	    List<IdiomExplan> explanList = new ArrayList<IdiomExplan>();
	    Log.d(TAG, idiomRight);
	    try {
            explanList = getFinalDb(context).findAllByWhere(IdiomExplan.class,"idiomRight like '" + idiomRight.trim() + "%'");
            if(explanList.size() > 0){
                return explanList.get(0).getIdiomExplan();
            }
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
	    return null;
	    
	}

}
